package com.sxbbc.plat.controller.jz;

import java.util.Collections;
import java.util.List;

import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exp.ExpDetailVo;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.enums.OrderStatusEnums;
import com.sxbbc.common.core.enums.OrderTypeEnums;
import com.sxbbc.common.core.enums.PayTypeEnums;
import com.sxbbc.common.core.enums.ReceivedTypeEnums;
import com.sxbbc.common.core.enums.RefundStatusEnums;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.plat.common.OrderMapping;
import com.sxbbc.plat.util.SystemLog;

/**
 * 订单管理控制层
 * @author xiaoky
 * @date 2019-05-09
 */
@Controller
public class OrderController {
	@Autowired
	private IOrderMainService orderMainService;
	@Autowired
	private IOrderRefundLogService orderRefundLogService;
	@Autowired
	private BBusinessService businessService;
	@Autowired
	private IOrderExpDetailService orderExpDetailService;
	@Autowired
	private IOrderDetailService orderDetailService;
	@Autowired
	private ICRechageDetailService rechageDetailService;
	
	/**
	 * 订单列表页面
	 * @param orderMain 查询条件
	 * @return ModelAndView
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.SHOW_ORDER_LIST)
	public ModelAndView showOrderList (OrderMain orderMain) {
		ModelAndView view = new ModelAndView(OrderMapping.SHOW_ORDER_LIST);
		// 订单类型
		List<OrderTypeEnums> typeList = OrderTypeEnums.queryValidList();
		// 订单状态
		List<OrderStatusEnums> statusList = OrderStatusEnums.queryList();
		// 支付方式
		List<PayTypeEnums> payTypeList = PayTypeEnums.queryList();
		// 店铺列表
		QueryWrapper<BBusiness> wrapper = new QueryWrapper<BBusiness>();
		wrapper.eq("on_off", StaticUtils.STATUS_NO)
			.eq("open", StaticUtils.STATUS_YES);
		List<BBusiness> businessList = businessService.list(null);
		view.addObject("typeList", typeList);
		view.addObject("statusList", statusList);
		view.addObject("payTypeList", payTypeList);
		view.addObject("businessList", businessList);
		return view;
	}
	
	/**
	 * 查询订单列表数据
	 * @param page
	 * @param orderMain
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.QUERY_ORDER_LIST)
	@ResponseBody
	public com.sxbbc.common.basics.rest.RestResponse queryOrderList (BasePage<OrderMain> page, OrderMain orderMain) {
		// 查询未删除的订单
		orderMain.setDel(StaticUtils.STATUS_NO);
		// 订单列表
		orderMainService.queryOrder(page, orderMain);
		return RestFactory.success(page.getRecords(), page.getTotal());
	}
	
	/**
	 * 订单详情页面
	 * @param orderId 订单ID
	 * @return ModelAndView
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.SHOW_ORDER_DETAIL)
	public ModelAndView showOrderDetail (Integer orderId) {
		ModelAndView view = new ModelAndView(OrderMapping.SHOW_ORDER_DETAIL);
		OrderMain order = orderMainService.getOrderDetailById(orderId);
		QueryWrapper<OrderExpDetail> wrapper = new QueryWrapper<>();
		wrapper.eq("order_code",order.getOrderCode());
		List<OrderExpDetail> expList = orderExpDetailService.list(wrapper);
		for (OrderExpDetail exp : expList) {
			if(exp.getReceivedType() != null){
				exp.setReceivedTypeName(ReceivedTypeEnums.getName(exp.getReceivedType()));
			}
		}
//		if(order.getOrderType().equals(OrderTypeEnums.RECHARGE_PHONEANDOIL.getId())){
//			QueryWrapper<CRechageDetail> rechageDetailQueryWrapper = new QueryWrapper<>();
//			rechageDetailQueryWrapper.eq("ordercode",order.getOrderCode());
//			CRechageDetail detail = rechageDetailService.getOne(rechageDetailQueryWrapper);
//			view.addObject("rechargeDetail", detail);
//		}
		order.setExpList(expList);
		if (expList.size() > 0 && expList != null) {
			order.setExpDetail(expList.get(0));
		}
		view.addObject("order", order);
		return view;
	}
	
	/**
	 * 订单退款管理
	 * @return ModelAndView
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.SHOW_ORDER_REFUND)
	public ModelAndView showOrderRefund () {
		ModelAndView view = new ModelAndView(OrderMapping.SHOW_ORDER_REFUND);
		return view;
	}
	
	/**
	 * 退款列表
	 * @param page 分页
	 * @param refundLog 查询数据
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.QUERY_REFUND_LIST)
	@ResponseBody
	public com.sxbbc.common.basics.rest.RestResponse queryRefundList (BasePage<OrderRefundLog> page, OrderRefundLog refundLog) {
		QueryWrapper<OrderRefundLog> wrapper = new QueryWrapper<OrderRefundLog>();
		if (refundLog!=null) {
			if (!StringUtils.isEmpty(refundLog.getOrderCode())) {
				wrapper.eq("order_code", refundLog.getOrderCode());
			}
			if (refundLog.getStatus() != null) {
				wrapper.eq("status", refundLog.getStatus());
			}
		}
		wrapper.orderByDesc("create_time");
		IPage<OrderRefundLog> refundPage = orderRefundLogService.queryRefunds(page, wrapper);
		return RestFactory.success(refundPage.getRecords(), refundPage.getTotal());
	}
	
	/**
	 * 退款详情
	 * @param refundId 退款id
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.SHOW_REFUND_DETAIL)
	public ModelAndView showOrderRefund (Integer refundId) {
		ModelAndView view = new ModelAndView(OrderMapping.SHOW_REFUND_DETAIL);
		OrderRefundLog refund = orderRefundLogService.getById(refundId);
		OrderDetail orderDetail = orderDetailService.getById(refund.getOrderDetailId());
		view.addObject("refund", refund);
		view.addObject("orderDetail", orderDetail);
		view.addObject("refundStatus", RefundStatusEnums.queryList());
		return view;
	}
	
	/**
	 * 退款驳回操作
	 * @param refundId 退款数据id
	 * @param rejectRemark 驳回理由
	 * @return RestResponse
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.HANDLE_REFUND_REJECT)
	@ResponseBody
	@SystemLog(methods = "退款驳回", module = "订单管理")
	public RestResponse handleRefundRefect (Integer refundId, String rejectRemark) {
		RestResponse result = orderRefundLogService.handleRefundRefect(refundId, rejectRemark, 0);
		return result;
	}
	
	/**
	 * 退款通过操作
	 * @param refundId 退款数据id
	 * @return RestResponse
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.HANDLE_REFUND_SUCCESS)
	@ResponseBody
	@SystemLog(methods = "退款通过", module = "订单管理")
	public RestResponse handRefundSuccess (Integer refundId) {
		RestResponse result = orderRefundLogService.handleRefundSuccess(refundId, 0);
		return result;
	}
	
	/**
	 * 通过订单编号查询物流信息
	 * @param orderCode 订单编号
	 * @return 物流数据信息
	 */
	@RequestMapping(value = OrderMapping.QUERY_EXP_MSG)
	@ResponseBody
	public RestResponse queryExpMsg (String orderCode) {
		OrderExpDetail expDetail = orderExpDetailService.getByOrderCode(orderCode);
		if (expDetail != null && CalcuUtils.getInstance().isEquals(expDetail.getReceivedType(), ReceivedTypeEnums.EXP.getId())) {
			String expMsg = orderExpDetailService.handleLiveExpData(expDetail.getId());
            List<ExpDetailVo> expMsgList = JSONArray.parseArray(expMsg, ExpDetailVo.class);
            if (expMsgList != null && expMsgList.size() > 0) {
                Collections.reverse(expMsgList);
            }
			expDetail.setExpDetailList(expMsgList);
		}
		return GetRest.getSuccess("", expDetail);
	}
	
	/**
	 * 显示订单物流信息
	 * @param orderCode 订单编号
	 * @return
	 * @author xiaoky
	 */
	@RequestMapping(value = OrderMapping.SHOW_ORDER_EXP_MSG)
	public ModelAndView showExpMsg (String orderCode) {
		ModelAndView view = new ModelAndView(OrderMapping.SHOW_ORDER_EXP_MSG);
		QueryWrapper<OrderExpDetail> wrapper = new QueryWrapper<>();
		wrapper.eq("order_code",orderCode);
		List<OrderExpDetail> expList = orderExpDetailService.list(wrapper);
		for (OrderExpDetail expDetail : expList) {
			if (expDetail != null && CalcuUtils.getInstance().isEquals(expDetail.getReceivedType(), ReceivedTypeEnums.EXP.getId())) {
				String expMsg = orderExpDetailService.handleLiveExpData(expDetail.getId());
	            List<ExpDetailVo> expMsgList = JSONArray.parseArray(expMsg, ExpDetailVo.class);
	            if (expMsgList != null && expMsgList.size() > 0) {
	                Collections.reverse(expMsgList);
	            }
				expDetail.setExpDetailList(expMsgList);
			}
		}
		view.addObject("expList", expList);
		return view;
	}
	
}
